import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @Author: wuzhenbin
 * @CreateTime: 2025-02-07
 */
public class leetcode_15 {
    public static void main(String[] args) {

    }

    //暴力没法过
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if(nums[i] + nums[j] + nums[k] ==0){
                        ArrayList<Integer> integers = new ArrayList<>();
                        integers.add(nums[i]);
                        integers.add(nums[j]);
                        integers.add(nums[k]);
                        integers.sort(Integer::compareTo);
                        if(!list.contains(integers)){
                            list.add(integers);
                        }
                    }
                }
            }
        }
        return list;
    }

    public static List<List<Integer>> threeSum1(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        // 先排序数组
        Arrays.sort(nums);

        for (int i = 0; i < nums.length - 2; i++) {
            // 跳过重复的元素，避免生成重复的三元组
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            // 双指针
            int left = i + 1;
            int right = nums.length - 1;

            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == 0) {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    // 跳过重复的元素
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++;
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    right--;
                }
            }
        }

        return result;
    }


    public List<List<Integer>> threeSum2(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        //双指针 就是为了规避双层循环 ......不要写成forfor  o(n)的
        for (int i = 0; i < nums.length-2; i++) {
            if(i>0&&nums[i]==nums[i-1])continue;
            int left = i + 1;
            int right = nums.length -1;
            while(left<right){
                if(nums[i] + nums[left] + nums[right] ==0){
                    list.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left ++;
                    right --;
                }

                else if(nums[i] + nums[left] + nums[right] <0)left++;
                else right--;
            }
        }
        return list;
    }
}
